/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.persistence.hibernate.core;

import com.blandware.atleap.common.Constants;
import com.blandware.atleap.common.util.ClassNameHolder;
import com.blandware.atleap.model.core.ActionPage;
import com.blandware.atleap.model.core.ContentPage;
import com.blandware.atleap.persistence.core.LookupDAO;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


/**
 * <p>Hibernate implementation of LookupDAO.
 * </p>
 * <p><a href="LookupDAOHibernate.java.html"><i>View Source</i></a>
 * </p>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @author Roman Puchkovskiy <a href="mailto:roman.puchkovskiy@blandware.com">
 *         &lt;roman.puchkovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.27 $ $Date: 2006/08/03 10:07:39 $
 */
public class LookupDAOHibernate extends BaseDAOHibernate implements LookupDAO {

	/**
	 * @see com.blandware.atleap.persistence.core.LookupDAO#listRoleTitles()
	 */
	public List listRoleTitles() {
		if ( log.isDebugEnabled() ) {
			log.debug("retrieving all role titles...");
		}
		return executeFind("select r.title from Role r order by r.title");
	}

	/**
	 * @see com.blandware.atleap.persistence.core.LookupDAO#listRoles()
	 */
	public List listRoles() {
		if ( log.isDebugEnabled() ) {
			log.debug("retrieving all roles...");
		}
		return executeFind("select r from Role r order by r.title");
	}

	/**
	 * @see com.blandware.atleap.persistence.core.LookupDAO#listRolesAssignedToLinkableItems()
	 */
	public List listRolesAssignedToLinkableItems() {
		if ( log.isDebugEnabled() ) {
			log.debug("retrieving roles assigned to linkable items...");
		}
		return executeFind("select distinct r from Page as page inner join page.roles as r where page.className != '" + ActionPage.class.getName() + "' and page.className != '" + ContentPage.class.getName() + "' order by r.title");
	}

	/**
	 * @see com.blandware.atleap.persistence.core.LookupDAO#listRolesAssignedToContentPages()
	 */
	public List listRolesAssignedToContentPages() {
		if ( log.isDebugEnabled() ) {
			log.debug("retrieving roles assigned to content pages...");
		}
		return executeFind("select distinct r from ContentPage as page inner join page.roles as r order by r.title");
	}

	/**
	 * @see com.blandware.atleap.persistence.core.LookupDAO#listRolesAssignedToDocuments()
	 */
	public List listRolesAssignedToDocuments() {
		if ( log.isDebugEnabled() ) {
			log.debug("retrieving roles assigned to documents...");
		}
		return executeFind("select distinct r from ContentDocument as document inner join document.roles as r order by r.title");
	}

	/**
	 * @see com.blandware.atleap.persistence.core.LookupDAO#listRolesAssignedToFiles()
	 */
	public List listRolesAssignedToFiles() {
		if ( log.isDebugEnabled() ) {
			log.debug("retrieving roles assigned to content resources...");
		}
		return executeFind("select distinct r from ContentResource as resource inner join resource.roles as r where resource.type = '" + Constants.RESOURCE_TYPE_FILE + "' order by r.title");
	}

	/**
	 * @see com.blandware.atleap.persistence.core.LookupDAO#listRolesAssignedToImages()
	 */
	public List listRolesAssignedToImages() {
		if ( log.isDebugEnabled() ) {
			log.debug("retrieving roles assigned to images...");
		}
		return executeFind("select distinct r from ContentImage as image inner join image.roles as r order by r.title");
	}

	/**
	 * @see com.blandware.atleap.persistence.core.LookupDAO#listRolesAssignedToContentResources()
	 */
	public List listRolesAssignedToContentResources() {
		if ( log.isDebugEnabled() ) {
			log.debug("retrieving roles assigned to content resources...");
		}
		return executeFind("select distinct r from ContentResource as resource inner join resource.roles as r order by r.title");
	}

	/**
	 * @see com.blandware.atleap.persistence.core.LookupDAO#listFreeRolesAssignedToUsers()
	 */
	public List listFreeRolesAssignedToUsers() {
		if ( log.isDebugEnabled() ) {
			log.debug("retrieving roles assigned to users as free roles...");
		}
		return executeFind("select distinct r from User as u inner join u.rolesAssociations as fra left outer join fra.role as r where fra.group is null order by r.title");
	}

	/**
	 * @see com.blandware.atleap.persistence.core.LookupDAO#listRolesAssignedToNewsItems()
	 */
	public List listRolesAssignedToNewsItems() {
		if ( log.isDebugEnabled() ) {
			log.debug("retrieving roles assigned to news items...");
		}
		return executeFind("select distinct r from NewsItem as item inner join item.roles as r order by r.title");
	}

	/**
	 * @see com.blandware.atleap.persistence.core.LookupDAO#listRolesAssignedToForums()
	 */
	public List listRolesAssignedToForums() {
		if ( log.isDebugEnabled() ) {
			log.debug("retrieving roles assigned to forums...");
		}
		return executeFind("select distinct r from Forum as f inner join f.roles as r order by r.title");
	}

	/**
	 * @see com.blandware.atleap.persistence.core.LookupDAO#listGroups() ()
	 */
	public List listGroups() {
		if ( log.isDebugEnabled() ) {
			log.debug("retrieving all groups...");
		}
		return executeFind("select g from Group g order by g.title");
	}

	/**
	 * @see com.blandware.atleap.persistence.core.LookupDAO#listLayoutNames()
	 */
	public List listLayoutNames() {
		if ( log.isDebugEnabled() ) {
			log.debug("retrieving all layout names...");
		}
		return executeFind("select l.name from Layout l order by l.name");
	}

	/**
	 * @see com.blandware.atleap.persistence.core.LookupDAO#listLayouts()
	 */
	public List listLayouts() {
		if ( log.isDebugEnabled() ) {
			log.debug("retrieving all layouts...");
		}
		return executeFind("select l from Layout l order by l.name");
	}

	/**
	 * @see com.blandware.atleap.persistence.core.LookupDAO#listContentResourceTypes()
	 */
	public List listContentResourceTypes() {
		if ( log.isDebugEnabled() ) {
			log.debug("retrieving all content resource types...");
		}
		return executeFind("select distinct r.type from ContentResource r order by r.type");
	}

	/**
	 * @see com.blandware.atleap.persistence.core.LookupDAO#listContentResourceMimeTypes()
	 */
	public List listContentResourceMimeTypes() {
		if ( log.isDebugEnabled() ) {
			log.debug("retrieving all content resource mime-types...");
		}
		return executeFind("select distinct r.mimeType from ContentResource r where r.mimeType is not null order by r.mimeType");
	}

    /**
     * @see com.blandware.atleap.persistence.core.LookupDAO#listLinkableItemsClassNames()
     */
    public List listLinkableItemsClassNames() {
        String hql = new StringBuffer("select page.className from Page page ")
                .append("where (page.className != '")
                .append(ActionPage.class.getName())
                .append("') and (page.className != '")
                .append(ContentPage.class.getName())
                .append("') ")
                .append("group by page.className ")
                .toString();
        List list = executeFind(hql);
        List result = new ArrayList();
        if (list != null) {
            for (Iterator i = list.iterator(); i.hasNext(); ) {
                String className = (String) i.next();
                String simpleClassName = "";
                if (className != null) {
                    int dotPos = className.lastIndexOf('.');
                    if (dotPos != -1) {
                        simpleClassName = className.substring(dotPos + 1);
                    }
                }
                ClassNameHolder holder = new ClassNameHolder();
                holder.setClassName(className);
                holder.setSimpleClassName(simpleClassName);
                result.add(holder);
            }
        }
        return result;
    }
}
